VPutSn = Put_Text_B_Length


Check_Cursor_Over_Building:

	;This will check to see if the cursor is over a building, including one under
	;construction

 ;If the user is placing a building, it will be confusing if that player is looking at HP while
 ;putting a building down.  So nothing will be shown if a player is choosing where
 ;to place a structure on the map.

 BIT Object_Ready_To_Add, (IY + flag)
 ret nz


 ;Finds what tile the cursor is pointing to

 call Find_Tile_On_Map



 ld a, (hl)		;Register A contains the tile we want to check, the tile
			;that the cursor is currently pointing to

 cp 126			;If the cursor is pointing to a building under
			;construction

 jr z, Pre_Draw_Building_Info	;Special care must be taken
				;for a building under construction.

 ;Since the tile is not a wrench, the cursor is over a full building

 RES Cursor_Over_Wrench, (IY + flag + 1)

 cp 48				;There are 48 building tiles, 0 to 47.
				;Is the tile 47 or less?

 ret nc				;Return if the cursor is not pointing over a building

 ld hl, Operation_Center 	;Check to see which building the tile
				;corresponds to.  The "Start tile" for the
				;Operation Center is at Operation_Center

 ld b, 16	;16 buildings to check

 ld c, a	;Save the value of the tile
_

 	
 ld e, (hl)	;We save the value of the start tile, and see if the
		;current tile falls with in this range.

 push de	;Register e contains the starting tile.

 ld de, 26

 add hl, de	;HL now points to the last tile required to draw a building.  Some buildings require
		;1 tile, some 2, and some 4.
 ld a, (hl)	;Building end tile
 or a
 sbc hl, de	;Return to the first tile required to draw the building

 pop de		;Recalls our value for register e

 inc hl

 ld d, a
 ld a, c

;See if the value in register a falls between E and D

 inc d

 cp e
 jr c, _
 cp d
 jr c, Draw_Building_Info



_
 
 ld de, 31
 add hl, de

 djnz   --_

 ret

 
Pre_Draw_Building_Info:

 SET Cursor_Over_Wrench, (IY + flag + 1) 

 ld de, List_Of_Buildings_Under_Construction

 ld a, (End_Of_List_Of_Buildings_Under_Construction)
 ld b, a


EmergencyLocalLabel9:


	ld a, (de)
	rla \ rla
	ld hl, Structure_Locations

	add a,l
	ld l,a
	adc a,h  
	sub l  
	ld h, a
	
	inc hl
	inc hl
	ld a, (hl)
	dec hl
	dec hl
	push de
	push hl

	 and %11110000
 	rra \ rra \ rra \ rra
 	ld e, a
	 ld d, 0
 	ld a, 32
 	Call Multiply_DE_By_A
 	ex de, hl
 	ld hl, Operation_Center
 	add hl, de

	inc hl
	ld c, (hl)
	
 	pop hl
	
	ld a,  (TileX)


 	ld d, a

 	ld a, (TileY)

 

 	ld e, a

 	call Check_Building_Where_Cursor
	pop de
	pop de
	

	inc de
	inc de
	inc de
	inc de

	djnz EmergencyLocalLabel9

	ret

Draw_Building_Info:
 
 ld a, (hl)	;Building type.  16 x 16, 32 x 32, etc.

 ld bc, 4	
		
 add hl, bc	;The next two bytes at hl will be the total HP of the building.

 ld d, (hl)
 inc hl
 ld e, (hl)
 inc hl
 inc hl		;HL points to the text for the building

 ld (Building_Total_HP), de	;Saves the total HP so that it can be recalled quickly


 ld d, a	;Register D now contains the type of building, 1-3




 push hl	;We need to retrieve the building that the tile points to.
		;Save hl for later.

 ld b, 200	;Two hundred buildings to search

 


 ld hl, Structure_Locations	;The list of all buildings on the map

	ld a, (TileX)

   	ld c, d	;Now Register C contains the building type   



	ld d, a   ;D contains the X coordinate of the cursor
	ld a, (TileY)      
	ld e, a   ;E contains the Y coordinate of the cursor


_

 call Check_Building_Where_Cursor




 inc hl
 inc hl
 inc hl
 inc hl
 

 djnz -_

 ret

Building_Under_Cursor_Found_PopDE:

 pop de

 BIT Cursor_Over_Wrench, (IY + flag + 1)
 jr z, _

 pop de
 push hl

_

Building_Under_Cursor_Found:

 push hl	;Save where the building is so we can retrieve the HP after
 		;putting the info box on the screen


 ld ix, InfoBox
 ld A, 12
 ld (Sprite_Width), A
 ld A, 0
 ld l, 48
 ld b, 15
 call Put_Sprite_Overwrite 

_ 


 pop hl		;hl points to the beginning of the building.  Retrieve the HP.

 push hl
 inc hl
 inc hl		;Get HP

 ld a, (hl)
 and %00001111
 ld d, a
 inc hl
 ld e, (hl)




 

 
 
 ld hl, 50 * 256 + 2
 ld (penCol), hl 




 pop bc

 pop hl ;Points to first line of text
 push de	;Saves the HP

 BIT Cursor_Over_Wrench, (IY + flag + 1) 
 jr z, +_


 
 pop de
 ld h, b
 ld l, c
 inc hl
 inc hl
 

 ld a, (hl)
 and %11110000
 rra \ rra \ rra \ rra
 ld e, a
 ld d, 0
 ld a, 32
 Call Multiply_DE_By_A
 ex de, hl
 ld hl, Operation_Center
 add hl, de
 ld de, 5
 add hl, de

 ld d, (hl)
 inc hl
 ld e, (hl)
 inc hl
 inc hl

 ld (Building_Total_HP), de
 push de
 
_


 ld b, 18
 call VPutSN




 ld hl, 56*256+2		;Friendly or not?
 ld (penCol), hl
 ld b, 8
 ld hl, Friendly_Unit
 call VPutSN

 ld hl, 56*256+60		;"HP:"
 ld (penCol), hl
 ld b, 6
 ld hl, HP_Text
 call VPutSN


 ;Now display the hp

_

Draw_Different_HP:

 ld hl, ClearText
 ld de, ASCIINumber
 ld bc, 3
 ldir
 pop de
 push de

 call bintobcd16
 ld hl, 56*256+72		
 ld (penCol), hl
 
 ld hl, ASCIINumber

 Call Put_Text_Zero_End
 

 pop hl
 
 ld a, h
 ld c, l

  sla c \ rla
  sla c \ rla
  sla c \ rla
  sla c \ rla
  sla c \ rla

 ld de, (Building_Total_HP)
 
 call Divide_AC_By_DE

 ld d, a
 ld e, c

 ld a, 96


 call Multiply_DE_By_A

 ex de, hl

 srl d \ rr e
 srl d \ rr e
 srl d \ rr e
 srl d \ rr e
 srl d \ rr e




 ld d, e
 ld e, 0
 
 push de

 ld a, 12
 ld (Sprite_Width), a
 ld b, 5

 ld a, 0
 ld l, 0

 ld ix, Progress_Health_Bar
 call Put_Sprite_Overwrite

 pop de

 ld a, d
 and %11111000
 rrca \ rrca \ rrca
 ld c, a
 ld a, 12
 sub c

 ld b, 5
 ld c, a

 ld ix, Progress_Bar_Clear
 ld iy, Progress_Bar_Clear

 call LargeClippedSprite

 ld iy, flags
 ret

ClearText:

 .db "///"


Divide_AC_By_DE:
   ld	hl, 0
   ld	b, 16

_loop:
   scf \ rl c

   rla
   adc	hl, hl
   sbc	hl, de
   jr	nc, $+4
   add	hl, de
   dec	c
   
   djnz	_loop
   
   ret

Progress_Health_Bar:

	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %00110000,%11000011,%00001100,%00110000,%11000011,%00001100,%00110000,%11000011,%00001100,%00110000,%11000011,%00001101
	.db %01100001,%10000110,%00011000,%01100001,%10000110,%00011000,%01100001,%10000110,%00011000,%01100001,%10000110,%00011001
	.db %11000011,%00001100,%00110000,%11000011,%00001100,%00110000,%11000011,%00001100,%00110000,%11000011,%00001100,%00110001
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111

Progress_Bar_Clear:

	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111




Check_Building_Where_Cursor:


 push de	;Save the X/Y tile location, since it's needed for every building being checked
  

 ld a, (hl)	;A = The X Location (in tiles) of the building being checked



 cp d		;If the tiles match, check the Y tiles
 jr z, _

 ld a, c	;Register A now contains the building type
 cp 3		;16 x 16 building
 jr z, ++_		
 cp 2		;16 x 32 building
 jr z, +_ 

 ld a, (hl)
 inc a
 cp d
 jr z, ++_
_

 inc hl
 ld a, (hl)
 dec hl
 cp e
 jp z, Building_Under_Cursor_Found_PopDE
 
 ld a, c
 cp 3
 jr z, _
 cp 1
 jr z, _

 inc hl
 ld a, (hl)
 dec hl
 inc a
 cp e
 jp z, Building_Under_Cursor_Found_PopDE
_ 
 pop de
 ret